package com.urea.system.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.urea.common.exception.ServiceException;
import com.urea.common.utils.StringUtils;
import com.urea.system.domain.vo.FleetTerminalVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.urea.system.mapper.FleetTerminalMapper;
import com.urea.system.domain.FleetTerminal;
import com.urea.system.service.IFleetTerminalService;

/**
 * 车队绑定的终端Service业务层处理
 * 
 * @author liuzd
 * @date 2023-08-01
 */
@Service
public class FleetTerminalServiceImpl extends ServiceImpl<FleetTerminalMapper, FleetTerminal> implements IFleetTerminalService
{
    @Autowired
    private FleetTerminalMapper fleetTerminalMapper;

    /**
     * 查询车队绑定的终端
     * 
     * @param id 车队绑定的终端主键
     * @return 车队绑定的终端
     */
    @Override
    public FleetTerminal selectFleetTerminalById(Long id)
    {
        return fleetTerminalMapper.selectFleetTerminalById(id);
    }

    /**
     * 查询车队绑定的终端列表
     * 
     * @param fleetTerminal 车队绑定的终端
     * @return 车队绑定的终端
     */
    @Override
    public List<FleetTerminal> selectFleetTerminalList(FleetTerminal fleetTerminal)
    {
        return fleetTerminalMapper.selectFleetTerminalList(fleetTerminal);
    }

    /**
     * 新增车队绑定的终端
     * 
     * @param vo 车队绑定的终端
     */
    @Override
    public void insertFleetTerminal(FleetTerminalVo vo)
    {
        String terminalIds = vo.getTerminalIds();
        if (StringUtils.isBlank(terminalIds)){
            throw new ServiceException("设备id不可为空");
        }
        String[] split = terminalIds.split(",");
        for (String s : split) {
            FleetTerminal fleetTerminal = new FleetTerminal();
            fleetTerminal.setFleetId(vo.getFleetId());
            fleetTerminal.setTerminalId(Long.parseLong(s));
            fleetTerminal.setDiscount(vo.getDiscount());
            fleetTerminal.setCreatedTime(LocalDateTime.now());
            this.save(fleetTerminal);
        }
    }

    /**
     * 修改车队绑定的终端
     * 
     * @param fleetTerminal 车队绑定的终端
     * @return 结果
     */
    @Override
    public int updateFleetTerminal(FleetTerminal fleetTerminal)
    {
        return fleetTerminalMapper.updateFleetTerminal(fleetTerminal);
    }

    /**
     * 批量删除车队绑定的终端
     * 
     * @param ids 需要删除的车队绑定的终端主键
     * @return 结果
     */
    @Override
    public int deleteFleetTerminalByIds(Long[] ids)
    {
        return fleetTerminalMapper.deleteFleetTerminalByIds(ids);
    }

    /**
     * 删除车队绑定的终端信息
     * 
     * @param id 车队绑定的终端主键
     * @return 结果
     */
    @Override
    public int deleteFleetTerminalById(Long id)
    {
        return fleetTerminalMapper.deleteFleetTerminalById(id);
    }

    /**
     * 根据用户id和终端id判断用户是否在终端车队下
     *
     * @param userId     用户id
     * @param terminalId 终端id
     * @return 结果
     */
    @Override
    public FleetTerminalVo checkUserInFleet(Long userId, Long terminalId) {
        return baseMapper.checkUserInFleet(userId,terminalId);
    }

    /**
     * 根据终端id批量查询终端绑定的车队信息
     *
     * @param tIds 终端ids
     * @return 结果
     */
    @Override
    public List<FleetTerminalVo> queryTerminalFleetInfoByTId(List<Long> tIds) {
        return baseMapper.queryTerminalFleetInfoByTId(tIds);
    }
}
